package cvc.framework.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cvc.framework.entity.CustomerMes;
import cvc.framework.entity.WebSocketMessage;
import cvc.framework.result.WebSocketResult;
import cvc.framework.webSocket.WebSocket;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.UUID;

@Service
public class DevicesService {


    public JSONArray getDevices(String targetName, String uuid) {
        WebSocketResult socketResult = new WebSocketResult();
        socketResult.code = "getDevices";
        socketResult.uuid = uuid;
        boolean ws = WebSocket.sendMessageToUser(targetName, socketResult.toString());
        try {
            if (ws) {
                int a = 0;
                while (a < 25) {    //  5秒钟超时
                    boolean contains = CustomerMes.customerMessage.containsKey(uuid);
                    if (contains) {
                        String getDevicesUUID = CustomerMes.customerMessage.get(uuid);
                        JSONArray jsonArray = JSONArray.parseArray(getDevicesUUID);
                        CustomerMes.customerMessage.remove(uuid);
                        return jsonArray;
                    }
                    Thread.sleep(200);
                    a++;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }


    public JSONObject getDeviceById(String targetName, String id, String uuid) {
        WebSocketResult socketResult = new WebSocketResult();
        socketResult.code = "getDeviceById";
        socketResult.uuid = uuid;
        socketResult.message = id;
        boolean ws = WebSocket.sendMessageToUser(targetName, socketResult.toString());
        try {
            if (ws) {
                int a = 0;
                while (a < 25) {    //  5秒钟超时
                    boolean contains = CustomerMes.customerMessage.containsKey(uuid);
                    if (contains) {
                        String getDeviceByIdUUID = CustomerMes.customerMessage.get(uuid);
                        JSONObject jsonObject = JSONObject.parseObject(getDeviceByIdUUID);
                        CustomerMes.customerMessage.remove(uuid);
                        return jsonObject;
                    }
                    Thread.sleep(200);
                    a++;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int addDevice(WebSocketMessage webSocketMessage) {
        String targetName = webSocketMessage.getTargetName();
        String uuid = webSocketMessage.getUuid();
        JSONObject message = webSocketMessage.getMessage();
        WebSocketResult socketResult = new WebSocketResult();
        socketResult.code = "addDevice";
        socketResult.uuid = uuid;
        socketResult.message = message;
        boolean ws = WebSocket.sendMessageToUser(targetName, socketResult.toString());
        try {
            if (ws) {
                int a = 0;
                while (a < 25) {    //  5秒钟超时
                    boolean contains = CustomerMes.customerMessage.containsKey(uuid);
                    if (contains) {
                        String addDeviceUUID = CustomerMes.customerMessage.get(uuid);
                        int code = Integer.parseInt(addDeviceUUID);
                        CustomerMes.customerMessage.remove(uuid);
                        return code;
                    }
                    Thread.sleep(200);
                    a++;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return 0;
    }


    public int updateDevice(WebSocketMessage webSocketMessage) {
        String targetName = webSocketMessage.getTargetName();
        String uuid = webSocketMessage.getUuid();
        JSONObject message = webSocketMessage.getMessage();
        WebSocketResult socketResult = new WebSocketResult();
        socketResult.code = "updateDevice";
        socketResult.uuid = uuid;
        socketResult.message = message;
        boolean ws = WebSocket.sendMessageToUser(targetName, socketResult.toString());
        try {
            if (ws) {
                int a = 0;
                while (a < 25) {    //  5秒钟超时
                    boolean contains = CustomerMes.customerMessage.containsKey(uuid);
                    if (contains) {
                        String updateDeviceUUID = CustomerMes.customerMessage.get(uuid);
                        int code = Integer.parseInt(updateDeviceUUID);
                        CustomerMes.customerMessage.remove(uuid);
                        return code;
                    }
                    Thread.sleep(200);
                    a++;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return 0;
    }


    public int deleteDevice(String targetName, String uuid, String id) {
        WebSocketResult socketResult = new WebSocketResult();
        socketResult.code = "deleteDevice";
        socketResult.uuid = uuid;
        socketResult.message = id;
        boolean ws = WebSocket.sendMessageToUser(targetName, socketResult.toString());
        try {
            if (ws) {
                int a = 0;
                while (a < 25) {    //  5秒钟超时
                    boolean contains = CustomerMes.customerMessage.containsKey(uuid);
                    if (contains) {
                        String deleteDeviceUUID = CustomerMes.customerMessage.get(uuid);
                        int code = Integer.parseInt(deleteDeviceUUID);
                        CustomerMes.customerMessage.remove(uuid);
                        return code;
                    }
                    Thread.sleep(200);
                    a++;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return 0;
    }


    public JSONObject getParamNames(String targetName, String uuid, String id) {
        WebSocketResult socketResult = new WebSocketResult();
        socketResult.code = "getParamNames";
        socketResult.uuid = uuid;
        socketResult.message = id;
        boolean ws = WebSocket.sendMessageToUser(targetName, socketResult.toString());
        try {
            if (ws) {
                int a = 0;
                while (a < 100) {    //  20秒钟超时
                    boolean contains = CustomerMes.customerMessage.containsKey(uuid);
                    if (contains) {
                        String getDeviceByIdUUID = CustomerMes.customerMessage.get(uuid);
                        JSONObject jsonObject = JSONObject.parseObject(getDeviceByIdUUID);
                        CustomerMes.customerMessage.remove(uuid);
                        return jsonObject;
                    }
                    Thread.sleep(200);
                    a++;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }


    public JSONObject modifyParams(WebSocketMessage webSocketMessage) {
        String targetName = webSocketMessage.getTargetName();
        String uuid = webSocketMessage.getUuid();
        JSONObject message = webSocketMessage.getMessage();
        WebSocketResult socketResult = new WebSocketResult();
        socketResult.code = "modifyParams";
        socketResult.uuid = uuid;
        socketResult.message = message;
        boolean ws = WebSocket.sendMessageToUser(targetName, socketResult.toString());
        try {
            if (ws) {
                int a = 0;
                while (a < 100) {    //  20秒钟超时
                    boolean contains = CustomerMes.customerMessage.containsKey(uuid);
                    if (contains) {
                        String updateDeviceUUID = CustomerMes.customerMessage.get(uuid);
                        JSONObject jsonObject = JSONObject.parseObject(updateDeviceUUID);
                        CustomerMes.customerMessage.remove(uuid);
                        return jsonObject;
                    }
                    Thread.sleep(200);
                    a++;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }
}
